<?php
class pdoMysql_dr extends pdoDb{
	private static $ins=null;
	private $cfg=array();
	private $link=null;
	private $stmt=null;
	private $error_code=0;
	private $error_info='';
	private $data;
	
	protected function __construct(){
		$conf=Conf::getIns();
		$this->cfg=$conf->load_config();
		$this->connect();
	}
	public function __destruct(){
		$this->close();
	}
	public static function getIns(){
        if(!(self::$ins instanceof self)) {
            self::$ins=new self();
        }
        return self::$ins;
    }
	//mysql:host=localhost;dbname=test;port=3307
	protected function connect(){
		$config_db=$this->cfg['DB'];
		if(!isset($config_db['dsn'])) exit('db need dsn param!');
		try{
			$this->link=new PDO($config_db['dsn'],$config_db['user'],$config_db['pwd']);
			$this->link->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_WARNING);
			$this->link->exec("set names {$config_db['charset']}");
		}catch(PDOException $e){
			echo "数据库连接失败：".$e->getMessage();
			exit;
		}
	}
	public function query($query,$data=array()){
		$stmt=$this->link->prepare($query);
		$this->stmt=$stmt;
		$r=$stmt->execute($data);
		if($msg=$this->err(1)){
			$pos=$_SESSION['_debug_pos_'];
			$this->_log_err($pos.$msg.$query.serialize($data).'\n');
			unset($_SESSION['_debug_pos_']);
		}
		return $r;
	}
	//Execute an SQL statement and return the number of affected rows
	public function exec($query){
		$r=$this->link->exec($query);
		if($msg=$this->err(0)){
			$pos=$_SESSION['_debug_pos_'];
			$this->_log_err($pos.$msg.$query.'\n');
			unset($_SESSION['_debug_pos_']);
		}
		return $r;
	}
	//Executes an SQL statement, returning a result set as a PDOStatement object 
	public function simple_query($query){
		$r=$this->link->query($query);
		$this->stmt=$r;
		if($msg=$this->err(0)){
			$pos=$_SESSION['_debug_pos_'];
			$this->_log_err($pos.$msg.$query.'\n');
			unset($_SESSION['_debug_pos_']);
		}
		return $r;
	}
	public function err($is_statement){
		if($is_statement){
			$this->error_code=$this->stmt->errorCode();
			$this->error_info=implode(' ',$this->stmt->errorInfo());
		}else{
			$this->error_code=$this->link->errorCode();
			$this->error_info=implode(' ',$this->link->errorInfo());
		}
		if($this->error_code){
			return $this->error_code.':'.$this->error_info;
		}else{
			return false;
		}
	}
	//
	public function fetch_array($type){
		switch($type){
			case 'ASSOC':
				return $this->stmt->fetch(PDO::FETCH_ASSOC);
			break;
			case 'NUM':
				return $this->stmt->fetch(PDO::FETCH_NUM);
			break;
			case 'BOTH':
				return $this->stmt->fetch(PDO::FETCH_BOTH);
			break;
			default:
				return $this->stmt->fetch(PDO::FETCH_ASSOC);
			break;
		}
	}
	public function fetch_array_all($type){
		switch($type){
			case 'ASSOC':
				return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
			break;
			case 'NUM':
				return $this->stmt->fetchAll(PDO::FETCH_NUM);
			break;
			case 'BOTH':
				return $this->stmt->fetchAll(PDO::FETCH_BOTH);
			break;
			default:
				return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
			break;
		}
	}
	public function fetch_row(){
		return $this->stmt->fetch(PDO::FETCH_NUM);
	}
	public function fetch_assoc(){
		return $this->stmt->fetch(PDO::FETCH_ASSOC);
	}
	public function free_result() {
		return true;
	}
	public function insert_id() {
		return $this->link->lastInsertId();
	}
	public function affected_rows(){
		return $this->stmt->rowCount();
	}
	public function close(){
		return true;
	}
//
}